import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/in_app_webiew_example.screen.dart';
import 'package:app/components/my_webview.dart';
import 'package:app/components/nodata_page.dart';
import 'package:app/pages/document_module/file_controller.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_swipe_action_cell/core/cell.dart';
import 'package:get/get.dart';
import 'package:ripple_backdrop_animate_route/ripple_backdrop_animate_route.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../common/app_theme.dart';
import '../../common/colors.dart';
import 'my_folder_file_model.dart';
class MyChromeSafariBrowser extends ChromeSafariBrowser {
  @override
  void onOpened() {
    print("ChromeSafari browser opened");
  }

  // @override
  // void onCompletedInitialLoad(didLoadSuccessfully) {
  //   print("ChromeSafari browser initial load completed");
  // }

  @override
  void onClosed() {
    print("ChromeSafari browser closed");
  }
}

class FileView extends GetView{

  final ChromeSafariBrowser browser = MyChromeSafariBrowser();

  @override
  FileController controller = Get.put(FileController());



  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      floatingActionButton:

      FloatingActionButton(
        elevation: 0,
        backgroundColor: Colors.transparent,
        onPressed: () {
          RippleBackdropAnimatePage.show(
            context: context,
            child:Container(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  height: 100,
                  // color: Colors.red,
                  child: Row(
                    children: [

                      GestureDetector(
                        onTap: (){
                          Get.back();
                          /// 获取上传地址
                          controller.requestDataWithUpLoader(Get.arguments);
                          // controller.tapClickCreateDocument();
                          // Get.to(MyWebView('title', 'http://49.234.222.178:9097/index?res_model=documents.document&res_id=24&res_field=&company_id=1&create_uid=2&write_uid=2&access_token=430f89381edac608ea51365d4d7403dc&folder_id=0&name=file&offset=10'));
                        },
                        child: Container(
                          width: 100,
                          height: 100,
                          // color: Colors.grey,
                          alignment: Alignment.center,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset('images/file_folder.png',),
                              SizedBox(height: 5,),
                              Text('上传文件')
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            childFade: true,
            duration: 300,
            blurRadius: 20,
            bottomButton: Icon(Icons.clear,color: Colors.grey,),
            bottomHeight: 60,
            bottomButtonRotate: false,
          );
        },
        child: Image.asset('images/float_add_icon.png',width: 45,height: 45,),
      ),

      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text(''),
      ),
      body: GetBuilder<FileController>(builder: (_){
        return Column(
          children: [
            Container(
              height: 55,
              child: BrnSearchText(
                // focusNode: focusNode,
                controller: controller.searchTextEditingController,
                borderRadius: BorderRadius.all(Radius.circular(15)),
                // searchController: scontroller..isActionShow = true,
                onTextClear: () {
                  return false;
                },
                autoFocus: false,
                onActionTap: () {
                  // scontroller.isClearShow = false;
                  // scontroller.isActionShow = false;
                  // focusNode.unfocus();
                  // BrnToast.show('取消', context);
                },
                onTextCommit: (text) {
                  // BrnToast.show('提交内容 : $text', context);
                },
                onTextChange: (text) {
                  // BrnToast.show('输入内容 : $text', context);
                  controller.requestData(search: text);
                },
              ),
            ),
            Expanded(child:  EasyRefresh.custom(

             onRefresh: () => controller.onRefresh(),
             emptyWidget: controller.dataArr.isEmpty?NoDataPage():null,
             slivers: [
               SliverList(
                 delegate: _mySliverChildBuilderDelegate(),
               ),
             ],
           ))
          ],
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            MyFolderFileItemModel model = controller.dataArr[index];
        return SwipeActionCell(
          key: ObjectKey(controller.dataArr[index]),
          trailingActions: <SwipeAction>[
            SwipeAction(
                title: "删除",
                onTap: (CompletionHandler handler) async {
                  await handler(true);
                  controller.requestDataWithUnlink(type: '文件',document_id: model.id);
                  // list.removeAt(index);
                  // setState(() {});
                },
                color: Colors.red),
            SwipeAction(
                title: "分享",
                onTap: (CompletionHandler handler) async {
                  await handler(false);
                  // controller.requestDataWithUnlink(type: '文件',document_id: model.id);
                  // list.removeAt(index);
                  // setState(() {});
                  controller.requestDataWithShare(model.id);
                },
                color: Colors.orange),
            SwipeAction(
                title: "下载",
                onTap: (CompletionHandler handler) async {
                  await handler(false);
                  // controller.requestDataWithUnlink(type: '文件',document_id: model.id);
                  // list.removeAt(index);
                  // setState(() {});
                  // controller.requestDataWithShare(model.id);
                  // Get.to(InAppWebViewExampleScreen(model.url));

                  // await launch(model.url!);
                  await browser.open(
                    url:WebUri(model.url!),
                  );

                },
                color: AppColor.themeColor),
          ],
          child:  GestureDetector(
            onTap:(){
              Get.to(MyWebView('title', model.preview_url));
              // controller.tapClickDocument(index,model.id);
            },
            child:Container(
              color: Colors.white,
              child:  Column(
                children: [
                  Container(
                    color: Colors.white,
                    padding: EdgeInsets.only(left: 5),
                    height: 65,
                    child:Row(
                      children: [
                        Image.asset('images/file_icon.png'),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: Get.width-50,
                              // color: Colors.yellowAccent,
                              // margin: EdgeInsets.only(top: 10),
                              padding: EdgeInsets.only(left: 15,top: 0),
                              alignment: Alignment.centerLeft,
                              child: Text('${model.name}'),
                            ),
                            Container(
                              padding: EdgeInsets.only(left: 15,top: 5),
                              child: Text('${model.createDate}',style: appThemeData.textTheme.subtitle1,),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 0.5,
                    color: AppColor.lineColor,
                  )
                ],
              ),
            ),
          ),
        );
      },
      childCount: controller.dataArr.length,
    );
  }
}